/**
 * @(#) AddressMediator.as
 *
 * @date		2009. 02. 17.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	본인 인사기록 조회 - Mediator
 * 				본인의 인사기록을 조회하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.myprofile_sac.view
{
	import com.skt.common_hr.common.PsHrAlert;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.myprofile_sac.MyProfileFacade;
	import com.skt.common_hr.myprofile_sac.model.AddressProxy;
	import com.skt.common_hr.myprofile_sac.model.PostProxy;
	import com.skt.common_hr.myprofile_sac.view.components.Address;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class AddressMediator extends CommonMediator
	{
		public static const NAME:String = "AddressMediator"; 

		public const bundle:String = "MyProfile";

		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function AddressMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		public function initView():void
 		{
 			facade.registerProxy(new PostProxy());
 			view.addEventListener(Address.OkInput, onOkInput);
 			view.addEventListener(Address.CancelInput, onCancelInput);
 			view.addEventListener(Address.SearchPost, onSearchPost);
 			onClean();
 			
 			commonFacade.setStatusMessage("STATUS_EDIT_ADDRESS");
 		}
 		
 		private function onClean():void{
 			view.postArr.removeAll();
 			view.txtSearchAddr.text = view.SEARCHINFO_MSG;
 			view.txtAddrDetail.text = "";
 			view.selAddr.PFULL = "";
 			view.selAddr.PSTLZ = "";
 		}
 		

	



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					MyProfileFacade.LOAD_POST_COMPLETE,
					MyProfileFacade.LOAD_POST_FAILED,
					MyProfileFacade.SAVE_ADDRESS_COMPLETE,
					MyProfileFacade.SAVE_ADDRESS_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case MyProfileFacade.LOAD_POST_COMPLETE:
					view.postArr = (note.getBody() as PostProxy).postList;
					trace(view.postArr.length);
					if(view.postArr.length < 1) {
						CommonAlert.showMessage(bundle, " 검색된 우편번호가 없습니다.\n 찾고자 하시는 동이름을 정확하게 입력 합시시오.", null, "TITLE_SYSTEM_ERROR");
					}
					break;
				case MyProfileFacade.LOAD_POST_FAILED:
					trace("MyProfileFacade.LOAD_POST_FAILED: " + note.getBody().retCode + ", " + note.getBody().retText);
					CommonAlert.showMessage(bundle, "우편번호 검색중 오류가 발생했습니다.", null, "DB오류");
					break;
				case MyProfileFacade.SAVE_ADDRESS_COMPLETE:
					facade.sendNotification(MyProfileFacade.OK_ADDRESS_INPUT, this);
					break;
				case MyProfileFacade.SAVE_ADDRESS_FAILED:
 					CommonAlert.showMessage(bundle,
 											"("+(note.getBody() as AddressProxy).retCode+") " + (note.getBody() as AddressProxy).retText,
 											null,
 											"TITLE_SYSTEM_ERROR");
 					commonFacade.setStatusMessage("");
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}





 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/* 
		private function onOkInput(event:Event):void
		{
			if(checkValidateAddress()) {
				var proxy:AddressProxy = new AddressProxy();
				facade.registerProxy(proxy);
				proxy.save(Variables.myPernr, view.selAddr.PSTLZ, view.txtAddrDetail.text);
 				commonFacade.setStatusMessage("STATUS_UPDAING_ADDRESS");
			}
			else {
				//AlertHelper.showMessage("주소가 정확하게 입력되지 않았습니다.", "입력오류");
				CommonAlert.showMessage(bundle, 
										"ALERT_INVALID_ADDRESS", null,
										"TITLE_DEFAULT");
			}
		}
 */
  
 		private function onOkInput(e:Event):void
		{
			view.address.LOCAT = view.txtAddrDetail.text;
			
			if(validateAddress()) {
				var proxy:AddressProxy = new AddressProxy();
				facade.registerProxy(proxy);
				
				var table:Array = new Array;
				table.push(view.address);
				proxy.newSave(Variables.myPernr, DateUtil.today(), table, "2");
			} 
		}


		private function validateAddress():Boolean
		{
			if(StringUtil.NVL(view.address.PSTLZ )=="") {
				PsHrAlert.showMessage("우편번호 검색을 해 주십시오.");
				return false;
			}
			
			if(StringUtil.NVL(view.address.LOCAT)=="") {
				PsHrAlert.showMessage("상세주소를 입력 해 주십시오");
				return false;
			}
						  
			return true; 
		}	
		private function onCancelInput(event:Event):void
		{
			commonFacade.setStatusMessage("");
			facade.sendNotification(MyProfileFacade.CANCEL_ADDRESS_INPUT, this);
		}
		
		private function onSearchPost(event:Event):void
		{
			var proxy:PostProxy = facade.retrieveProxy(PostProxy.NAME) as PostProxy;
			if(view.txtSearchAddr.text.length>0) { 
				proxy.load(view.txtSearchAddr.text);
			}
			else {
				view.txtSearchAddr.setFocus();
				CommonAlert.showMessage(bundle, 
										"ALERT_INVALID_POST", null,
										"TITLE_DEFAULT");
			}
		}
		

		private function checkValidateAddress():Boolean
		{
			if(StringUtil.NVL(view.selAddr.PSTLZ)=="") {
				//return false;
			}
			if(view.txtAddrDetail.text.length <1) {
				return false;
			}
			return true;
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():Address
        {
            return viewComponent as Address;
        }
	}
}